<?php
/**
 * DataAccess
 * 
 * DataAccess implemented as singleton 
 * to connect to data sources.  
 * 
 * @author Gabriel PREDA
 * @copyright  Copyright (c) 2008-2009 Acceleron Team
 * @version    $Id: DataAccess.php 35 2008-12-02 20:58:03Z gabriel.preda $
 */

class DataAccess
{

/**
 * Singleton instance
 *
 * @var DataAccess
 */
private static $instance;

/**
 * Data source settings
 *
 * @var string
 */
private $DataSources = false;

/**
 * Database connection
 *
 * @var PDO
 */
public $DatabaseObj;

/**
 * Connection the the cache object
 *
 * @var Memcache
 */
public $MemCacheObj;

/**
 * Singleton
 *
 */
private function __construct()
{
	$this->GetConnectVariables();
	if (!isset($this->DataSources['Database']))
	{
		throw new DataAccessException(_('No database section defined in configuration file.'), 270);
	}
	
	$this->ConnectDb();

	$this->ConnectMemory();
	echo 6;
}

/**
 * Get the single instance of the connection class
 *
 * @return DataAccess
 */
public static function GetInstance()
{
	if (!isset(self::$instance))
	{
		$c = __CLASS__;
		self::$instance = new $c();
	}

	return self::$instance;
}

/**
 * Parse the file that contains data sources
 * 
 */
private function GetConnectVariables()
{
	$this->DataSources = parse_ini_file('data-sources.ini', true);
	if (false === $this->DataSources)
	{
		throw new DataAccessException(_('Unable to read configuration file.'), 270);
	}
}

/**
 * Real database connect
 *
 */
private function ConnectDb()
{
	if (!isset($this->DataSources['Database']))
	{
		throw new DataAccessException(_('No database section defined in the config file.'), 270);
	}

	$this->DatabaseObj = new DataAccessDatabase($this->DataSources['Database'], 
		$this->DataSources['Database-DriverOptions']
			? $this->DataSources['Database-DriverOptions']
			: null  
		);
}

/**
 * Connect to cache daemon
 *
 */
private function ConnectMemory()
{
	if (isset($this->DataSources['Memcache']))
	{
		$this->MemCacheObj = new DataAccessMemcache($this->DataSources['Memcache']);
	}
}


}

/**
 * DataAccess exception class
 * @internal error code 270 
 */
class DataAccessException extends AcceleronException
{

}

